﻿namespace IESN.Communication
{
    using Microsoft.VisualBasic; 
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using Microsoft.VisualBasic.CompilerServices;

    public class SimenAPI : IPLC
    {
        private short DBNo1;
        private short Status_LResetbit = 0;
        private short Status_LRread = 0;
        private short Status_LRwrite = 0;
        private short Status_LSetbit = 0;
        private short Status_NRead1 = 0;
        private short Status_NResetbit1 = 0;
        private short Status_NSetbit1 = 0;
        private short Status_NWrite1 = 0;
        private short Status_StatusRead = 0;
        private short Status_StatusWrite = 0;
        private short Status1;

        public SimenAPI(string pclAddrTable, short initConnCount)
        {
            load_tool((byte) initConnCount, "S7ONLINE", pclAddrTable);
            new_ss(1);
        }

        public static string BToC500(string bcode, short numberofbcodedigit)
        {
            short num2;
            string str3 = "";
            if (numberofbcodedigit <= 0)
            {
                return str3;
            }
            short num = Convert.ToInt16(bcode.Length);
            if (num > 0)
            {
                short num5 = num;
                for (num2 = 1; num2 <= num5; num2 = (short) (num2 + 1))
                {
                    //ou 加
                    //var aSubStr = bcode.Substring(num2 - 1, 1);
                    //var aAsc = ASCIIEncoding.ASCII.GetBytes(aSubStr.ToUpper());//只取第一个字符的ASCII码
                    //string str4 = aAsc[0].ToString();
                    string str4 = Conversion.Str(Strings.Asc(Strings.UCase(Strings.Mid(bcode, num2, 1))));//原有表达式
                    str4 = str4.Substring(str4.Length - 2, 2);
                    str3 = str3 + str4;
                }
            }
            if ((numberofbcodedigit % 2) == 0)
            {
                if (numberofbcodedigit > num)
                {
                    short num4 = num;
                    for (num2 = Convert.ToInt16((int) (numberofbcodedigit - 1)); num2 >= num4; num2 = (short) (num2 + -1))
                    { 
                        if (StringType.StrCmp(bcode, "", false) == 0)
                        {
                            str3 = str3 + "00";
                        }
                        else
                        {
                            str3 = str3 + "32";
                        }
                    }
                }
                return str3;
            }
            if ((numberofbcodedigit % 2) != 1)
            {
                return str3;
            }
            if (numberofbcodedigit > num)
            {
                short num3 = num;
                for (num2 = numberofbcodedigit; num2 >= num3; num2 = (short) (num2 + -1))
                { 
                    if (StringType.StrCmp(bcode, "", false) == 0)
                    {
                        str3 = str3 + "00";
                    }
                    else
                    {
                        str3 = str3 + "32";
                    }
                }
                return str3;
            } 
            if (StringType.StrCmp(bcode, "", false) == 0)
            {
                return (str3 + "00");
            }
            return (str3 + "32");
        }

        public void CloseComm()
        {
            unload_tool();
        }

        public static void CompressReadArryToDWordArry(ref byte[] ReadArry, ref long[] DWordArry)
        {
            long num7 = 0L;
            DWordArry = new long[(Information.UBound(ReadArry, 1) / 4) + 1];
            long num8 = Information.UBound(ReadArry, 1) / 4;
            for (long i = 0L; i <= num8; i += 1L)
            {
                double num2 = ReadArry[(int) (num7 + 0L)];
                double num3 = ReadArry[(int) (num7 + 1L)];
                double num4 = ReadArry[(int) (num7 + 2L)];
                double num5 = ReadArry[(int) (num7 + 3L)];
                double a = (((((num2 * 256.0) * 256.0) * 256.0) + ((num3 * 256.0) * 256.0)) + (num4 * 256.0)) + num5;
                num7 += 4L;
                DWordArry[(int) i] = (long) Math.Round(a);
            }
        }

        public static void CompressReadArryToWordArry(ref byte[] ReadArry, ref int[] WordArry)
        {
            long num4 = 0L;
            WordArry = new int[(Information.UBound(ReadArry, 1) / 2) + 1];
            long num6 = Information.UBound(ReadArry, 1) / 2;
            for (long i = 0L; i <= num6; i += 1L)
            {
                double num2 = ReadArry[(int) num4];
                double a = (num2 * 256.0) + ReadArry[(int) (num4 + 1L)];
                num4 += 2L;
                WordArry[(int) i] = (int) Math.Round(a);
            }
        }

        public static string ConvertByteArryToString(ref byte[] byteArray, int numberofbcodedigit)
        {
            string str2 = "";
            int num2 = numberofbcodedigit - 1;
            for (int i = 0; i <= num2; i++)
            {
                if (byteArray[i] == 0)
                {
                    str2 = str2 + " ";
                }
                else
                {
                    str2 = str2 + StringType.FromChar(Strings.Chr(byteArray[i]));
                }
            }
            return str2;
        }

        public static void CopySArryToDArry(ref byte[] SArry, ref byte[] DArry, int firstPoint, int copyLen)
        {
            DArry = new byte[(copyLen - 1) + 1];
            int index = 0;
            int num3 = (firstPoint + copyLen) - 1;
            for (int i = firstPoint; i <= num3; i++)
            {
                DArry[index] = SArry[i];
                index++;
            }
        }

        public static void CopyTmpByteArryToSendByteArry(ref byte[] tmpArry, ref byte[] SendArry)
        {
            bool flag = false;
            short num = 0;
            long num3 = Information.UBound(tmpArry, 1);
            long num4 = 0L;
            try
            {
                num = (short) Information.UBound(SendArry, 1);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                SendArry = new byte[((int) num3) + 1];
                num = (short) Information.UBound(SendArry, 1);
                flag = true;
                ProjectData.ClearProjectError();
            }
            if (num >= 1)
            {
                if (!flag)
                {
                    num4 = Information.UBound(SendArry, 1) + 1;
                    SendArry = (byte[]) Utils.CopyArray((Array) SendArry, new byte[((int) ((Information.UBound(SendArry, 1) + num3) + 1L)) + 1]);
                }
            }
            else
            {
                SendArry = new byte[((int) num3) + 1];
                flag = true;
            }
            long num5 = num3;
            for (long i = 0L; i <= num5; i += 1L)
            {
                SendArry[(int) num4] = tmpArry[(int) i];
                num4 += 1L;
            }
        }

        public static void CovertStringToByteArry(string bcode, short numberofbcodedigit, ref byte[] byteArray)
        {
            string str = BToC500(bcode, numberofbcodedigit);
            byteArray = new byte[(numberofbcodedigit - 1) + 1];
            int start = 1;
            int num3 = numberofbcodedigit - 1;
            for (int i = 0; i <= num3; i++)
            {
                byteArray[i] = (byte) Math.Round(Conversion.Val(Strings.Mid(str, start, 2)));
                start += 2;
            }
        }

        [DllImport("w95_s7.dll")]
        private static extern int d_field_read(int db, int nr, int anz, ref byte value);
        [DllImport("w95_s7.dll")]
        private static extern int d_field_write(int db, int nr, int anz, ref byte value);
        [DllImport("w95_s7.dll")]
        private static extern int db_buch(short value);
        [DllImport("w95_s7.dll")]
        private static extern int db_read(int db, int dw, ref int anz, ref short value);
        [DllImport("w95_s7.dll")]
        private static extern int db_write(int db, int dw, ref int anz, ref short value);
        public static byte DecConvert(string strDataString)
        {
            byte num;
            try
            {
                num = (byte) Math.Round(Conversion.Val("&H" + strDataString));
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                num = 0;
                ProjectData.ClearProjectError();
            }
            return num;
        }

        public static void getByteArray(long dblData, ref byte[] byteArray, string dataType)
        {
            int num;
            StringBuilder builder = new StringBuilder("");
            string expression = Strings.Trim(Conversion.Hex(dblData));
            int num2 = Strings.Len(expression);
            if ((num2 % 2) != 0)
            {
                expression = "0" + expression;
                num2++;
            }
            if (StringType.StrCmp(dataType, "W", false) == 0)
            {
                int num6 = 4 - num2;
                for (num = 1; num <= num6; num++)
                {
                    builder.Append("0");
                }
                builder.Append(expression);
                expression = builder.ToString();
                num2 = 4;
            }
            if (StringType.StrCmp(dataType, "DW", false) == 0)
            {
                int num5 = 8 - num2;
                for (num = 1; num <= num5; num++)
                {
                    builder.Append("0");
                }
                builder.Append(expression);
                expression = builder.ToString();
                num2 = 8;
            }
            byteArray = new byte[((int) Math.Round((double) ((((double) num2) / 2.0) - 1.0))) + 1];
            int start = 1;
            int num4 = (int) Math.Round((double) ((((double) num2) / 2.0) - 1.0));
            for (num = 0; num <= num4; num++)
            {
                string strDataString = Strings.Mid(expression, start, 2);
                byteArray[num] = DecConvert(strDataString);
                start += 2;
            }
        }

        [DllImport("w95_s7.dll")]
        private static extern int load_tool(byte nr, string dev, string adr);
        [DllImport("w95_s7.dll")]
        private static extern int new_ss(byte nr);
        public string NReadWord(short start_dm_address, short dm_count)
        {
            string str=string.Empty;
            return str;
        }

        public bool NReadWord(short start_dm_address, short dm_count, ref byte[] value_byte)
        {
            value_byte = new byte[((dm_count * 2) - 1) + 1];
            short anz = (short) (dm_count * 2);
            if (d_field_read(this.DBNo, start_dm_address, anz, ref value_byte[0]) != 0)
            {
                this.Status_NRead = 0;
                return false;
            }
            this.Status_NRead = 1;
            return true;
        }

        public string NReadWord(short start_dm_address, short dm_count, string DataType)
        {
            string str2 = "";
            byte[] readArry = new byte[((dm_count * 2) - 1) + 1];
            StringBuilder builder = new StringBuilder("");
            short index = 0;
            if (StringType.StrCmp(DataType, "", false) == 0)
            {
                DataType = "W";
            }
            short anz = (short) (dm_count * 2);
            if (d_field_read(this.DBNo, start_dm_address, anz, ref readArry[0]) != 0)
            {
                this.Status_NRead = 0;
                return "";
            }
            if (StringType.StrCmp(DataType, "W", false) == 0)
            {
                int[] numArray2=null;
                CompressReadArryToWordArry(ref readArry, ref numArray2);
                short num5 = (short) (numArray2.Length - 1);
                for (index = 0; index <= num5; index = (short) (index + 1))
                {
                    if (index == 0)
                    {
                        builder.Append(numArray2[index].ToString());
                    }
                    else
                    {
                        builder.Append("|" + numArray2[index].ToString());
                    }
                }
            }
            if (StringType.StrCmp(DataType, "DW", false) == 0)
            {
                long[] numArray=null;
                CompressReadArryToDWordArry(ref readArry, ref numArray);
                short num4 = (short) (numArray.Length - 1);
                for (index = 0; index <= num4; index = (short) (index + 1))
                {
                    if (index == 0)
                    {
                        builder.Append(numArray[index].ToString());
                    }
                    else
                    {
                        builder.Append("|" + numArray[index].ToString());
                    }
                }
            }
            if (StringType.StrCmp(DataType, "S", false) == 0)
            {
                str2 = ConvertByteArryToString(ref readArry, dm_count * 2);
                builder.Append(str2);
            }
            this.Status_NRead = 1;
            return builder.ToString();
        }

        public string NResetBit(short dmaddress, short bitnumber)
        {
            if ((bitnumber >= 0x10) & (bitnumber <= 0x1f))
            {
                dmaddress = (short) (dmaddress + 2);
                bitnumber = (short) (bitnumber - 0x10);
            }
            if ((bitnumber >= 0x20) & (bitnumber <= 0x2f))
            {
                dmaddress = (short) (dmaddress + 4);
                bitnumber = (short) (bitnumber - 0x20);
            }
            if ((bitnumber >= 0x30) & (bitnumber <= 0x3f))
            {
                dmaddress = (short) (dmaddress + 6);
                bitnumber = (short) (bitnumber - 0x30);
            }
            if ((bitnumber >= 0x40) & (bitnumber <= 0x4f))
            {
                dmaddress = (short) (dmaddress + 8);
                bitnumber = (short) (bitnumber - 0x40);
            }
            try
            {
                byte[] buffer = new byte[2];
                if (d_field_read(this.DBNo, dmaddress, 2, ref buffer[0]) != 0)
                {
                    this.Status_NResetbit = 0;
                    return "";
                }
                if ((bitnumber >= 0) & (bitnumber <= 7))
                {
                    buffer[1] = (byte) (buffer[1] & ((long) Math.Round((double) (255.0 - Math.Pow(2.0, (double) bitnumber)))));
                }
                if ((bitnumber >= 8) & (bitnumber <= 15))
                {
                    buffer[0] = (byte) (buffer[0] & ((long) Math.Round((double) (255.0 - Math.Pow(2.0, (double) (bitnumber - 8))))));
                }
                if (d_field_write(this.DBNo, dmaddress, 2, ref buffer[0]) != 0)
                {
                    this.Status_NResetbit = 0;
                    return "";
                }
                this.Status_NResetbit = 1;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.Status_NResetbit = 0;
                ProjectData.ClearProjectError();
            }
            return "";
        }

        public string NSetBit(short dmaddress, short bitnumber)
        {
            if ((bitnumber >= 0x10) & (bitnumber <= 0x1f))
            {
                dmaddress = (short) (dmaddress + 2);
                bitnumber = (short) (bitnumber - 0x10);
            }
            if ((bitnumber >= 0x20) & (bitnumber <= 0x2f))
            {
                dmaddress = (short) (dmaddress + 4);
                bitnumber = (short) (bitnumber - 0x20);
            }
            if ((bitnumber >= 0x30) & (bitnumber <= 0x3f))
            {
                dmaddress = (short) (dmaddress + 6);
                bitnumber = (short) (bitnumber - 0x30);
            }
            if ((bitnumber >= 0x40) & (bitnumber <= 0x4f))
            {
                dmaddress = (short) (dmaddress + 8);
                bitnumber = (short) (bitnumber - 0x40);
            }
            try
            {
                byte[] buffer = new byte[2];
                if (d_field_read(this.DBNo, dmaddress, 2, ref buffer[0]) != 0)
                {
                    this.Status_NSetbit = 0;
                    return "";
                }
                if ((bitnumber >= 0) & (bitnumber <= 7))
                {
                    buffer[1] = (byte) (buffer[1] | ((long) Math.Round(Math.Pow(2.0, (double) bitnumber))));
                }
                if ((bitnumber >= 8) & (bitnumber <= 15))
                {
                    buffer[0] = (byte) (buffer[0] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 8)))));
                }
                if (d_field_write(this.DBNo, dmaddress, 2, ref buffer[0]) != 0)
                {
                    this.Status_NSetbit = 0;
                    return "";
                }
                this.Status_NSetbit = 1;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                this.Status_NSetbit = 0;
                ProjectData.ClearProjectError();
            }
            return "";
        }

        public bool NWriteWord(short start_dm_address, short dm_count, byte[] SendArry)
        {
            dm_count = (short) SendArry.Length;
            if (d_field_write(this.DBNo, start_dm_address, dm_count, ref SendArry[0]) == 0)
            {
                this.Status_NWrite = 1;
                return true;
            }
            this.Status_NWrite = 0;
            return false;
        }

        public string NWriteWord(short start_dm_address, short dm_count, string Indata)
        {
            string str=string.Empty;
            return str;
        }

        public string NWriteWord(short start_dm_address, short dm_count, string Indata, string DataType)
        {
            int num=0;
            byte[] buffer2=null;
            int[] numArray=null;
            short anz = 0;
            short index = 0;
            if (StringType.StrCmp(DataType, "", false) == 0)
            {
                DataType = "W";
            }
            string[] strArray = Indata.Split(new char[] { '|' });
            dm_count = (short) strArray.Length;
            if (StringType.StrCmp(DataType, "S", false) != 0)
            {
                numArray = new int[(strArray.Length - 1) + 1];
                short num7 = (short) (strArray.Length - 1);
                for (index = 0; index <= num7; index = (short) (index + 1))
                {
                    numArray[index] = (int) Math.Round(Conversion.Val(strArray[index]));
                }
            }
            byte[] sendArry = new byte[1];
            if (StringType.StrCmp(DataType, "W", false) == 0)
            {
                short num6 = (short) (dm_count - 1);
                for (index = 0; index <= num6; index = (short) (index + 1))
                {
                    getByteArray((long) numArray[index], ref buffer2, "W");
                    CopyTmpByteArryToSendByteArry(ref buffer2, ref sendArry);
                }
                num = d_field_write(this.DBNo, start_dm_address, dm_count * 2, ref sendArry[0]);
            }
            if (StringType.StrCmp(DataType, "DW", false) == 0)
            {
                short num5 = (short) (dm_count - 1);
                for (index = 0; index <= num5; index = (short) (index + 1))
                {
                    getByteArray((long) numArray[index], ref buffer2, "DW");
                    CopyTmpByteArryToSendByteArry(ref buffer2, ref sendArry);
                }
                num = d_field_write(this.DBNo, start_dm_address, dm_count * 4, ref sendArry[0]);
            }
            if (StringType.StrCmp(DataType, "S", false) == 0)
            {
                short num4 = (short) (dm_count - 1);
                for (index = 0; index <= num4; index = (short) (index + 1))
                {
                    CovertStringToByteArry(strArray[index], (short) strArray[index].Length, ref buffer2);
                    CopyTmpByteArryToSendByteArry(ref buffer2, ref sendArry);
                    anz = (short) (anz + strArray[index].Length);
                }
                num = d_field_write(this.DBNo, start_dm_address, anz, ref sendArry[0]);
            }
            if (num == 0)
            {
                this.Status_NWrite = 1;
            }
            else
            {
                this.Status_NWrite = 0;
            }
            return "";
        }

        [DllImport("w95_s7.dll")]
        private static extern int unload_tool();
        public object writeBitArea(short startOffset, short startBitOffset, short bitSize, string WriteData)
        {
            object obj2=null;
            return obj2;
        }

        public short DBNo
        {
            get
            {
                return this.DBNo1;
            }
            set
            {
                this.DBNo1 = value;
            }
        }

        public short Status
        {
            get
            {
                return this.Status1;
            }
            set
            {
                this.Status1 = value;
            }
        }

        public short Status_NRead
        {
            get
            {
                return this.Status_NRead1;
            }
            set
            {
                this.Status_NRead1 = value;
            }
        }

        public short Status_NResetbit
        {
            get
            {
                return this.Status_NResetbit1;
            }
            set
            {
                this.Status_NResetbit1 = value;
            }
        }

        public short Status_NSetbit
        {
            get
            {
                return this.Status_NSetbit1;
            }
            set
            {
                this.Status_NSetbit1 = value;
            }
        }

        public short Status_NWrite
        {
            get
            {
                return this.Status_NWrite1;
            }
            set
            {
                this.Status_NWrite1 = value;
            }
        }

        //public short VBNET.HowTo.IPLC.DBNo
        //{
        //    get
        //    {
        //        return this.DBNo1;
        //    }
        //    set
        //    {
        //        this.DBNo1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status
        //{
        //    get
        //    {
        //        return this.Status1;
        //    }
        //    set
        //    {
        //        this.Status1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NRead
        //{
        //    get
        //    {
        //        return this.Status_NRead1;
        //    }
        //    set
        //    {
        //        this.Status_NRead1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NResetbit
        //{
        //    get
        //    {
        //        return this.Status_NResetbit1;
        //    }
        //    set
        //    {
        //        this.Status_NResetbit1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NSetbit
        //{
        //    get
        //    {
        //        return this.Status_NSetbit1;
        //    }
        //    set
        //    {
        //        this.Status_NSetbit1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NWrite
        //{
        //    get
        //    {
        //        return this.Status_NWrite1;
        //    }
        //    set
        //    {
        //        this.Status_NWrite1 = value;
        //    }
        //}
    }
}

